#pragma once
#include<string>
#include<unordered_map>

class HttpResponse{
public:
    int _statu_code;  //状态码
    bool _redirect_flag; 
    std::string _redirect_url;
    std::string _content_body;
    std::unordered_map<std::string, std::string> _headers;
public:
    HttpResponse():_redirect_flag(false),_statu_code(200){}
    HttpResponse(int statu):_redirect_flag(false),_statu_code(statu){}
    
    void Reset(){
        _statu_code = 200;
        _redirect_flag = false;
        _content_body.clear();
        _redirect_url.clear();
        _headers.clear();
    }

    // 插入头部字段
    void SetHeader(const std::string &key, const std::string &val)
    {
        _headers.insert(std::make_pair(key, val));
    }

    // 是否存在指定头部字段
    bool HasHeader(const std::string &key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
        {
            return false;
        }
        return true;
    }

    // 获取指定头部字段的值
    std::string GetHeader(const std::string &key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
        {
            return "";
        }
        return it->second;
    }

    void SetContent(const std::string &content, const std::string &type){
        _content_body = content;
        SetHeader("Content-Type", type);
    }

    void SetDirect(const std::string &url, int statu = 302){
        _statu_code = statu;
        _redirect_flag = true;
        _redirect_url = url;
    }

    bool IsClose(){
        // 没有Connection字段，或者有Connection但是值是close，则都是短链接，否则就是长连接
        if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive")
        {
            return false;
        }
        return true;
    }

};